package com.dxy.app.widget.shading_view;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;

/**
  *Created by duxingyu on 2017/7/24.18:50
  *Email: 
  *Description:仿微信切换界面时渐变色的tag
  **/

public class ShadingView extends LinearLayout implements ViewPager.OnPageChangeListener,View.OnClickListener{

  private ViewPager pager;

  private int screenWidth;
  /**
   * 表情名称
   * */
  private String[] tabNames;
  /**
   * 被选中时的图标
   * */
  private int [] tabSelectResIds;
  /**
   * 未被选中时的图标
   * */
  private int [] tabUnselectResIds;
  public ShadingView(Context context) {
    super(context);
  }
  public ShadingView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public ShadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  /**
   * 初始化
   * */
  private void init(){
    setOrientation(HORIZONTAL);
    setBackgroundColor(Color.WHITE);
    int length =tabNames.length;
    screenWidth =getContext().getResources().getDisplayMetrics().widthPixels;
    int eachWidth =screenWidth/length;
    for(int i =0;i<length;i++){
      ShadingItemView item =new ShadingItemView(getContext(),tabNames[i],tabSelectResIds[i],tabUnselectResIds[i]);
      item.setTag(i);
      item.setOnClickListener(this);
      LayoutParams lps =new LayoutParams(eachWidth,LayoutParams.WRAP_CONTENT);
      item.setLayoutParams(lps);
      addView(item);
      if(i==curIndex){
        item.setCurAlpha(255);
      }
    }
  }

  /**
   * 绑定viewpager
   * */
  public void setPager(ViewPager pager){
    this.pager =pager;
    pager.setOnPageChangeListener(this);

    from = (ShadingItemView) getChildAt(curIndex);
  }

  private int curIndex=0;
  public void setCurrentPager(int curPager){
    this.curIndex =curPager;
    pager.setCurrentItem(curPager);

  }

  public void setDataResource(String[] tags,int []resIdDef,int [] resIdSel){
    this.tabNames =tags;
    this.tabUnselectResIds =resIdDef;
    this.tabSelectResIds =resIdSel;
    init();
  }
  public static final String TAG="TAG_ShadingView";


  private ShadingItemView from,to,tempV;
  private int destIndex;
  /**
   * 在最后一次调用时position为新叶卡的索引
   * */
  @Override
  public synchronized void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    if(lockForClick){
      Log.i(TAG, "onPageScrolled: operation has delayed!!!");
      return;
    }
    if(positionOffsetPixels==0&&curIndex==position){//表示这是边缘页的拖拽，无效，直接返回
      Log.i(TAG, "onPageScrolled: "+"无效拖拽，返回!!!");
      return;
    }
    if(tempV ==null){
      if(screenWidth-positionOffsetPixels>positionOffsetPixels-0){//表示往左滑动
        Log.i(TAG, "onPageScrolled: "+"往左滑动,页码+++1");
        to =tempV = (ShadingItemView) getChildAt(curIndex+1);
        destIndex =curIndex+1;
      }else{//表示往右滑动
        Log.i(TAG, "onPageScrolled: "+"往右滑动,页码---1");
        to =tempV = (ShadingItemView) getChildAt(curIndex-1);
        destIndex =curIndex-1;
      }
    }
    float temp =positionOffsetPixels;
    float alpha =temp/screenWidth;
    //Log.i(TAG, "onPageScrolled: "+temp+"--"+position+"--"+alpha);
    from.setAlpha(alpha*255);

    Log.i(TAG, "onPageScrolled: "+to);
    if(to==null||destIndex != (int)to.getTag()){
      to = (ShadingItemView) getChildAt(destIndex);
      Log.i(TAG, "onPageScrolled: "+destIndex);
    }
    Log.i(TAG, "onPageScrolled: "+to);
    //to.setAlpha(255*(1-alpha));
    /*if(position!=curIndex){
      Log.i(TAG, "onPageScrolled: "+"最后一次更新状态,"+curIndex+","+position);
      curIndex =position;
      tempV =null;
    }*/
    /*if(to==null){
      Log.i(TAG, "onPageScrolled: "+"初始化。。。");
      if(positionOffsetPixels>screenWidth/2){//表示这是自左往右滑动，叶卡往右滑动
        Log.i(TAG, "onPageScrolled: "+"往右滑动!!!");
        to = (ShadingItemView) getChildAt(curIndex-1);
      }else{
        Log.i(TAG, "onPageScrolled: "+"往左滑动!!!");
        to = (ShadingItemView) getChildAt(curIndex+1);
      }
    }


    if(position!=curIndex){//最后一次时，要将状态还原
      Log.i(TAG, "onPageScrolled: "+"还原");
      curIndex =position;
      from.setCurAlpha(0);
      to.setCurAlpha(255);
      from =to;
      to =null;//还原，用于下次继续判断当前偏移方向
    }*/
  }

  /**
   * 只有当换页的时候才会触发这个方法
   * */
  @Override public void onPageSelected(int position) {
    Log.i(TAG, "onPageSelected: 更新状态"+position);
    if(!lockForClick){
      curIndex =position;
      tempV =null;
    }else{
    }
  }

  @Override public void onPageScrollStateChanged(int state) {

  }

  private boolean lockForClick =false;
  @Override public void onClick(View v) {
    int pos = (int) v.getTag();
    if(pos==curIndex){
      return;
    }
    lockForClick =true;
    reverseState(pos,curIndex);
    pager.setCurrentItem(pos);
  }
  private void reverseState(int selPos,int curPos){
    ShadingItemView newItem = (ShadingItemView) getChildAt(selPos);
    ShadingItemView oldItem = (ShadingItemView) getChildAt(curPos);

    newItem.setCurAlpha(255);
    oldItem.setCurAlpha(0);
    curIndex =selPos;
  }
}
